var Patch = require('../models/patch')
module.exports = function (app) {
	app.use(function (req, res, next) {
		next()
	})
	 // 添加补丁
	  app.post('/patch/createPatch', function (req, res) {

	    var _patch = req.body
	    console.log("创建补丁请求:"+_patch)
	    Patch.findOne({name: _patch.name}, function (err, patch) {
	      if (err) {
	        console.log(err)
	      }
	      if (patch) {
	        res.json({
	          errno: 1,
	          data: '补丁已存在'
	        })
	      } else {
	        var patch = new Patch(_patch)
	        patch.save(function (err, patch) {
	          if (err) {
	            console.log(err)
	          }
	          res.json({
	            errno: 0,
	            data: '创建成功'
	          })
	        })
	      }
	    })
	  })
    // 获取补丁
    app.post('/patch/getPatch', function (req, res) {
      console.log("获取补丁请求:"+req.body)
      var _patch = req.body
      var name = _patch.name
      Patch.findOne({name: name}, function (err, patch) {
        if (err) {
          console.log(err);
        }
        console.log(patch)
        if (!patch) {
          res.json({
            errno: 1,
            data: '补丁不存在'
          })
        } else {
          res.json({
              errno: 0,
              data: '补丁找到'
            })
        }

      })
    })
}